package BasicKnowledge.Map;

import java.util.*;

/**
 * @time: 2021/2/1/001 14:43
 * @author: Zhurunhe
 */

public class PCollectionMap {
    public static void main(String[] args) {
        //创建Map集合，存储<员工姓名,工资>
        //Map是一个接口
        Map<String,Integer> map=new HashMap<>();

        //添加数据 put(K,V)
        map.put("zhangsan1",100);
        map.put("zhangsan2",200);
        map.put("zhangsan3",300);
        map.put("zhangsan4",400);
        map.put("zhangsan5",500);
        map.put("zhangsan6",600);
        //打印出来的顺序与添加的顺序不一致
        System.out.println(map);//{zhangsan1=100, zhangsan3=300, zhangsan2=200, zhangsan5=500, zhangsan4=400, zhangsan6=600}

        //当键已存在，再次添加数据会更改之前的值
        map.put("zhangsan6",1000);
        System.out.println(map);//{zhangsan1=100, zhangsan3=300, zhangsan2=200, zhangsan5=500, zhangsan4=400, zhangsan6=1000}


        System.out.println("------------");
        //判断
        System.out.println(map.size());//<键,值>对数量
        System.out.println(map.containsKey("zhangsan2"));
        System.out.println(map.containsValue(10));
        //获取指定的某个人的值
        System.out.println(map.get("zhangsan1"));

        System.out.println("----------");
        //修改、替换
        map.replace("zhangsan1",200);//{zhangsan1=200, zhangsan3=300, zhangsan2=200, zhangsan5=500, zhangsan4=400, zhangsan6=1000}
        System.out.println(map);
        map.replace("ssss",1);//修改的键不存在，不发生变化
        System.out.println(map);

        //删除
        System.out.println("-----------");
        map.remove("zhangsan6");
        System.out.println(map);//{zhangsan1=200, zhangsan3=300, zhangsan2=200, zhangsan5=500, zhangsan4=400}


        System.out.println("-----------------");
        //返回所有键、值的集合
        Set<String> strings = map.keySet();
        System.out.println(strings);
        Collection<Integer> values = map.values();
        Iterator<Integer> iterator = values.iterator();
        while (iterator.hasNext()) {
            Integer integer=iterator.next();
            System.out.println(integer);
        }

        System.out.println("-------------");
        //返回所有entry集合，一个<K,V>就是一个entry
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry:entries) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
    }
}
